package com.kensure.controller;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import co.kensure.frame.ResultInfo;
import co.kensure.frame.ResultRowInfo;
import co.kensure.frame.ResultRowsInfo;
import co.kensure.http.RequestUtils;
import co.kensure.mem.PageInfo;

import com.alibaba.fastjson.JSONObject;
import com.kensure.shike.baobei.service.SKBaobeiService;
import com.kensure.shike.changshang.model.ChangShang;
import com.kensure.shike.changshang.service.ChangShangService;
import com.kensure.shike.order.model.SKFinPay;
import com.kensure.shike.order.model.SKOrder;
import com.kensure.shike.order.model.SKZffs;
import com.kensure.shike.order.query.SKFinPayQuery;
import com.kensure.shike.order.query.SKOrderQuery;
import com.kensure.shike.order.service.SKFinPayASyncService;
import com.kensure.shike.order.service.SKFinPayService;
import com.kensure.shike.order.service.SKOrderService;
import com.kensure.shike.user.model.SKLogin;
import com.kensure.shike.user.model.SKUser;
import com.kensure.shike.user.service.SKLoginService;
import com.kensure.shike.user.service.SKUserFRService;
import com.kensure.shike.user.service.SKUserService;

/**
 * 订单逻辑处理
 * 
 * @author fankaidi
 *
 */
@Controller
@RequestMapping(value = "order")
public class SKOrderController {

	@Resource
	private SKFinPayService sKFinPayService;
	@Resource
	private SKOrderService sKOrderService;
	@Resource
	private SKLoginService sKLoginService;
	@Resource
	private SKUserFRService sKUserFRService;
	@Resource
	private SKBaobeiService sKBaobeiService;
	@Resource
	private ChangShangService changShangService;
	@Resource
	private SKFinPayASyncService sKFinPayASyncService;
	@Resource
	private SKUserService sKUserService;

	/**
	 * 前端，订单新增
	 */
	@ResponseBody
	@RequestMapping(value = "create.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo create(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		SKFinPay finpay = JSONObject.parseObject(json.toJSONString(), SKFinPay.class);
		SKFinPay finpay1 = sKFinPayService.add(finpay);
		return new ResultRowInfo(finpay1);
	}

	/**
	 * 前端，根据提交的数据，生成订单数据
	 */
	@ResponseBody
	@RequestMapping(value = "preorder.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo preorder(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		SKUser user = sKUserService.getUser();
		SKFinPay finpay = JSONObject.parseObject(json.toJSONString(), SKFinPay.class);
		SKFinPay order = sKFinPayService.preView(finpay.getOrderList(),user,false);
		return new ResultRowInfo(order);
	}

	/**
	 * 前端，根据id，获取详情
	 */
	@ResponseBody
	@RequestMapping(value = "detail.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo detail(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Long id = json.getLong("id");
		SKFinPay order = sKFinPayService.detail(id);
		return new ResultRowInfo(order);
	}

	/**
	 * 前端，用余额后者别的进行支付
	 */
	@ResponseBody
	@RequestMapping(value = "pay.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo pay(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Long id = json.getLong("id");
		Double money = json.getDouble("money");
		Integer type = json.getInteger("type");
		sKFinPayASyncService.yuePay(id, money, type);
		return new ResultRowInfo();
	}

	/**
	 * 前端，根据id，获取支付信息
	 */
	@ResponseBody
	@RequestMapping(value = "getzffs.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo getZffs(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Long id = json.getLong("id");
		SKZffs order = sKFinPayService.getZffs(id);
		return new ResultRowInfo(order);
	}

	/**
	 * 前端，根据id，获取详情,orderdetail
	 */
	@ResponseBody
	@RequestMapping(value = "orderdetail.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo orderdetail(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Long id = json.getLong("id");
		SKOrder order = sKOrderService.detail(id);
		return new ResultRowInfo(order);
	}

	/**
	 * 前端，退换货
	 */
	@ResponseBody
	@RequestMapping(value = "saveback.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo saveback(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Long orderid = json.getLong("orderid");
		Integer typeid = json.getInteger("typeid");
		String remark = json.getString("remark");
		String pics = json.getString("pics");
		sKOrderService.back(orderid, typeid, remark, pics);
		return new ResultRowInfo();
	}

	/**
	 * 前端，根据用户获取订单
	 */
	@ResponseBody
	@RequestMapping(value = "list.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo list(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Integer status = json.getInteger("status");
		List<SKFinPay> list = sKFinPayService.qdlist(status);
		return new ResultRowsInfo(list);
	}

	/**
	 * 前端，确认收货
	 */
	@ResponseBody
	@RequestMapping(value = "shouhuo.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo shouhuo(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Long id = json.getLong("id");
		sKFinPayService.shouhuo(id);
		return new ResultRowInfo();
	}

	/**
	 * 前端，关闭订单
	 */
	@ResponseBody
	@RequestMapping(value = "close.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo close(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Long id = json.getLong("id");
		sKFinPayService.close(id);
		return new ResultRowInfo();
	}

	/**
	 * 后端，获取订单
	 */
	@ResponseBody
	@RequestMapping(value = "hdlist.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo hdlist(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		SKFinPayQuery query = JSONObject.parseObject(json.toJSONString(), SKFinPayQuery.class);
		PageInfo page = JSONObject.parseObject(json.toJSONString(), PageInfo.class);
		List<SKFinPay> list = sKFinPayService.hdlist(query, page);
		long count = sKFinPayService.hdlistCount(query);
		return new ResultRowsInfo(list, count);
	}

	/**
	 * 后端，同意退换货
	 */
	@ResponseBody
	@RequestMapping(value = "tongyiback.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo tongyiback(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Long id = json.getLong("id");
		sKOrderService.tongYiBack(id);
		return new ResultRowInfo();
	}

	/**
	 * 后端，拒绝退换货
	 */
	@ResponseBody
	@RequestMapping(value = "jujueback.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo jujueback(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Long id = json.getLong("id");
		sKOrderService.juJueBack(id);
		return new ResultRowInfo();
	}

	/**
	 * 后端，设置快递单号
	 */
	@ResponseBody
	@RequestMapping(value = "kddh.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo kddh(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Long id = json.getLong("id");
		String kddh = json.getString("kddh");
		sKFinPayService.kddh(id, kddh);
		return new ResultRowInfo();
	}

	/**
	 * 后端，统计营销概况
	 */
	@ResponseBody
	@RequestMapping(value = "yingxiaoshuju.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo yingxiaoshuju(HttpServletRequest req, HttpServletResponse rep) {
		List<Map<String, Object>> list = sKOrderService.yingxiaoshuju(null);
		return new ResultRowsInfo(list);
	}

	/**
	 * 后端，单个商品统计营销概况
	 */
	@ResponseBody
	@RequestMapping(value = "yingxiaosp.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo yingxiaosp(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Long id = json.getLong("id");
		List<Map<String, Object>> list = sKOrderService.yingxiaosp(id);
		return new ResultRowsInfo(list);
	}

	/**
	 * 后端，统计营销详情
	 */
	@ResponseBody
	@RequestMapping(value = "yingxiaoxq.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo yingxiaoxq(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		SKOrderQuery query = JSONObject.parseObject(json.toJSONString(), SKOrderQuery.class);
		PageInfo page = JSONObject.parseObject(json.toJSONString(), PageInfo.class);
		List<Map<String, Object>> list = sKOrderService.yingxiaoxq(query, page);
		long total = sKBaobeiService.selectCountByWhere(null);
		return new ResultRowsInfo(list, total);
	}

	/**
	 * 后端，商品利润核算
	 */
	@ResponseBody
	@RequestMapping(value = "splrhs.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo splrhs(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		SKOrderQuery query = JSONObject.parseObject(json.toJSONString(), SKOrderQuery.class);
		PageInfo page = JSONObject.parseObject(json.toJSONString(), PageInfo.class);
		List<Map<String, Object>> list = sKOrderService.splrhs(query, page);
		long total = sKBaobeiService.selectCountByWhere(null);
		return new ResultRowsInfo(list, total);
	}

	/**
	 * 商家端，获取订单
	 */
	@ResponseBody
	@RequestMapping(value = "sjlist.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo sjlist(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		String tokenid = json.getString("tokenid");
		SKLogin login = sKLoginService.selectByTokenId(tokenid);
		List<ChangShang> changshang = changShangService.selectByUserId(login.getUserid());
		Long sjid = changshang.get(0).getId();

		SKFinPayQuery query = JSONObject.parseObject(json.toJSONString(), SKFinPayQuery.class);
		query.setStartStatus(1);
		// 商家id
		query.setSjid(sjid);
		PageInfo page = JSONObject.parseObject(json.toJSONString(), PageInfo.class);
		List<SKFinPay> list = sKFinPayService.sjlist(query, page);
		long count = sKFinPayService.sjlistCount(query);
		return new ResultRowsInfo(list, count);
	}

	/**
	 * 后端，销售入账
	 */
	@ResponseBody
	@RequestMapping(value = "xsrz.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo xsrz(HttpServletRequest req, HttpServletResponse rep) {
		sKUserFRService.commitZhang();
		return new ResultRowInfo();
	}

	/**
	 * 后端，付款测试
	 */
	@ResponseBody
	@RequestMapping(value = "fktest.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo fktest(HttpServletRequest req, HttpServletResponse rep) {
		sKFinPayASyncService.asyncAfterFk();
		return new ResultRowInfo();
	}
}
